package com.guli.gulikiller.killer.service.impl;

import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.gulikiller.killer.dao.StockOrderDao;
import com.guli.gulikiller.killer.entity.StockEntity;
import com.guli.gulikiller.killer.entity.StockOrderEntity;
import com.guli.gulikiller.killer.service.StockOrderService;
import com.guli.gulikiller.killer.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("stockOrderService")
public class StockOrderServiceImpl extends ServiceImpl<StockOrderDao, StockOrderEntity> implements StockOrderService {
    @Autowired
    private  StockService stockService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StockOrderEntity> page = this.page(
                new Query<StockOrderEntity>().getPage(params),
                new QueryWrapper<StockOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public int createWrongOrder(Integer sid) {
        //校验库存
        StockEntity stock = checkStock(sid);
        int id =0;
        if(stock!=null){
            //扣库存
            boolean count = saleStock(stock);
            //创建订单
            if(count)id = createOrder(stock);
        }
        return id;
    }


    public StockEntity checkStock(int sid) {
        StockEntity stock = stockService.getById(sid);
        if (stock.getSale().equals(stock.getCount())) {
                System.out.println("库存不足");
                return null;
        }
        return stock;
    }
    @Transactional
    public boolean saleStock(StockEntity stock) {
        stock.setSale(stock.getSale() + 1);
        return stockService.updateById(stock);
    }
    @Transactional
    public int createOrder(StockEntity stock) {
        StockOrderEntity order = new StockOrderEntity();
        order.setSid(stock.getId());
        order.setName(stock.getName());
        order.setCreateTime(new Date());
        int id = this.insertSelective(order);
        return id;
    }
    @Transactional
    @Override
    public int insertSelective(StockOrderEntity order) {
        this.baseMapper.insert(order);
        return order.getId();
    }

    @Override
    @Transactional
    public int createOptimisticOrder(Integer sid) {
        //校验库存
        StockEntity stock = checkStock(sid);
        int id=0;
        if(stock!=null){
            //扣库存
            int count =saleStockOptimistic(stock);
            //创建订单
            if(count>0)id = createOrder(stock);
        }
        return id;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int createSadOrder(Integer sid) {
        //校验库存
        StockEntity stock = checkStock(sid);
        int id =0;
        if(stock!=null){
            //扣库存
            boolean count = saleStock(stock);
            //创建订单
            if(count)id = createOrder(stock);
        }
        return id;
    }

    public int saleStockOptimistic(StockEntity stock) {
        System.out.println("查询数据库，尝试更新库存");
        int count = this.baseMapper.updateStockByOptimistic(stock);
        if (count == 0){
            System.out.println("并发更新库存失败，version不匹配") ;
        }
        return  count;
    }
}